package views

import (
	"errors"
	"go_web/common"
	"go_web/service"
	"log"
	"net/http"
	"strconv"
	"strings"
)

func (h *HTMLApi) Index(w http.ResponseWriter, r *http.Request) {

	index := common.Template.Index

	if err := r.ParseForm(); err != nil {
		log.Println("ParseForm err")
		index.WriteError(w, errors.New("系统出错 ParseForm "))
		return
	}

	//获取page
	pageStr := r.Form.Get("page")

	page := 1

	if pageStr != "" {
		page, _ = strconv.Atoi(pageStr)
	}
	//获取每页显示数量
	pageSize := 5

	path := r.URL.Path
	slug := strings.TrimPrefix(path, "/")

	hr, err := service.GetAllIndexInfo(slug, page, pageSize)

	//fmt.Println(hr)
	if err != nil {
		log.Println("首页获取数据出错 GetAllIndexInfo")
		index.WriteError(w, errors.New("系统出错 GetAllIndexInfo"))
	}

	index.WriteData(w, hr)

}
